package alo.p6.work5;

import alo.p6.BinarySearchTreeNode;

/**
 * @ClassName BuildHB0
 * @Description 给定高度h，产生完全二叉树
 * @Author Fanxc
 * @DATE 2021/3/8 22:08
 * @Version 1.0
 */
public class BuildHB0 {
    int count=0;//全局变量
    BinarySearchTreeNode BuildHB0(int h){
        BinarySearchTreeNode temp;
        if (h==0){
            return null;
        }
        temp=new BinarySearchTreeNode();
        temp.setLeft(BuildHB0(h-1));
        temp.setData(count++);
        temp.setRight(BuildHB0(h-1));
        return temp;
    }
    //使用归并的方法，不使用全局变量，但是不是使用高度，而是使用范围
    BinarySearchTreeNode BuildHB1(int l,int h){
        BinarySearchTreeNode temp;
        int mid=l+(h-1)/2;
        if (l>h){
            return null;
        }
        temp=new BinarySearchTreeNode();
        temp.setData(mid);
        temp.setLeft(BuildHB1(l,mid-1));
        temp.setRight(BuildHB1(mid+1,h));
        return temp;
    }

}
